package jlfm.lastfm.apps.recorder;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class M3UWritingListener implements ProgressListener {

	private File m3uFile;

	public M3UWritingListener(File m3uFile) {
		this.m3uFile = m3uFile;
	}

	public void progressFinished(ProgressEvent e) {
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(new FileWriter(m3uFile, true));
			writer.write(((FileWritingStreamHandler) e.getSource())
					.getFileNameProvider().getOutputFile(e.getMetadata())
					.getAbsolutePath()
					+ "\n");

		} catch (IOException exc) {
			exc.printStackTrace();
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	public void progressStarted(ProgressEvent e) {
		try {
			m3uFile.createNewFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

	public void progressUpdated(ProgressEvent e) {
	}

}
